using System;
using System.Collections.Generic;

// 定义一个接口，包含计算面积和判断合法性的方法
public interface IShape
{
    double Area { get; }
    bool IsValid { get; }
}

// 定义一个抽象类，实现 IShape 接口
public abstract class Shape : IShape
{
    public abstract double Area { get; }
    public abstract bool IsValid { get; }
}

// 长方形类
public class Rectangle : Shape
{
    public double Length { get; set; }
    public double Width { get; set; }

    public Rectangle(double length, double width)
    {
        Length = length;
        Width = width;
    }

    public override double Area
    {
        get
        {
            return Length * Width;
        }
    }

    public override bool IsValid
    {
        get
        {
            return Length > 0 && Width > 0;
        }
    }
}

// 正方形类，继承自长方形类
public class Square : Rectangle
{
    public Square(double side) : base(side, side)
    {
    }
}

// 三角形类
public class Triangle : Shape
{
    public double SideA { get; set; }
    public double SideB { get; set; }
    public double SideC { get; set; }

    public Triangle(double sideA, double sideB, double sideC)
    {
        SideA = sideA;
        SideB = sideB;
        SideC = sideC;
    }

    public override double Area
    {
        get
        {
            if (!IsValid)
            {
                return 0;
            }
            double s = (SideA + SideB + SideC) / 2;
            return Math.Sqrt(s * (s - SideA) * (s - SideB) * (s - SideC));
        }
    }

    public override bool IsValid
    {
        get
        {
            return SideA > 0 && SideB > 0 && SideC > 0 &&
                   SideA + SideB > SideC &&
                   SideA + SideC > SideB &&
                   SideB + SideC > SideA;
        }
    }
}

// 简单工厂类
public class ShapeFactory
{
    private static Random random = new Random();

    public static IShape CreateRandomShape()
    {
        int shapeType = random.Next(3);
        switch (shapeType)
        {
            case 0:
                // 创建长方形
                double length = random.NextDouble() * 10 + 1;
                double width = random.NextDouble() * 10 + 1;
                return new Rectangle(length, width);
            case 1:
                // 创建正方形
                double side = random.NextDouble() * 10 + 1;
                return new Square(side);
            case 2:
                // 创建三角形
                double sideA = random.NextDouble() * 10 + 1;
                double sideB = random.NextDouble() * 10 + 1;
                double sideC = random.NextDouble() * 10 + 1;
                return new Triangle(sideA, sideB, sideC);
            default:
                throw new ArgumentException("Invalid shape type");
        }
    }
}

class Program
{
    static void Main()
    {
        List<IShape> shapes = new List<IShape>();
        // 随机创建 10 个形状对象
        for (int i = 0; i < 10; i++)
        {
            IShape shape = ShapeFactory.CreateRandomShape();
            shapes.Add(shape);
        }

        double totalArea = 0;
        // 计算这些对象的面积之和
        foreach (IShape shape in shapes)
        {
            if (shape.IsValid)
            {
                totalArea += shape.Area;
            }
        }

        Console.WriteLine($"The total area of the 10 shapes is: {totalArea}");
    }
}